﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;

namespace MReviewerClient.Providers
{
    /// <summary>
    /// The manager for playing sound in application
    /// </summary>
    public class SoundProvider
    {
        /// <summary>
        /// The instance of the class
        /// </summary>
        private static SoundProvider instance;

        /// <summary>
        /// The lock object to prevent multi process play the sound at the same time
        /// </summary>
        private static object syncLock = new object();

        /// <summary>
        /// The singleton pattern to guarantee only an object will be created
        /// </summary>
        public static SoundProvider Instance
        {
            get
            {
                lock (syncLock)
                {
                    if (instance == null)
                        instance = new SoundProvider();
                    return instance;
                }
            }
        }

        /// <summary>
        /// The method for playing a sound
        /// </summary>
        /// <param name="path"></param>
        public void PlaySound(string path)
        {
            if (!string.IsNullOrEmpty(path))
            {
                using (var stream = TitleContainer.OpenStream(path))
                {
                    if (stream != null)
                    {
                        var effect = SoundEffect.FromStream(stream);
                        FrameworkDispatcher.Update();
                        effect.Play();
                    }
                }
            }
        }
    }
}
